/**
  * 排行榜
  */
module game {

    export class OpenSafeBoxMediator extends BaseMediator {
        public static NAME: string = "OpenSafeBoxMediator";
        public constructor(viewComponent: any = null) {
            super(OpenSafeBoxMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_SAFEBOX,
                PanelNotify.HALL.REFRESH_SAFEBOX,
                PanelNotify.HALL.PASS_SAFEBOX,
                PanelNotify.HALL.CLOSE_SAFEBOX
            ];
        }
        private panel: OpenSafeBoxPanel;
        private TrackWidth = 630; //这个值是皮肤上黄色进度条的原始长度，滑动音乐调节时根据此值*百分比计算新的长度
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_SAFEBOX: {
                    this.panel = null;
                    this.panel = new OpenSafeBoxPanel();
                    this.initBankInfo(notification.getBody());
                    this.showUI(this.panel, 2, 1, true, false);
                    break;
                }
                case PanelNotify.HALL.REFRESH_SAFEBOX: {
                    this.refresh(notification.getBody());
                    break;
                }
                case PanelNotify.HALL.PASS_SAFEBOX: {
                    this.passSet(notification.getBody());
                    break;
                }
                case PanelNotify.HALL.CLOSE_SAFEBOX: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            if (GameVO.getVo().cbInsureEnabled) {
                this.panel.srMima.visible = true;
            }
            else {
                this.panel.szMima.visible = true;
            }
            this.panel.btn_ensure.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onEnsureBtnClickl, this)
            this.panel.btn_getIdentifycode.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGetIdentufyCodeBtnClick, this);
            this.panel.btn_ensure0.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onEnsureBtnClick, this)
            this.panel.btn_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.hideSetMima, this)
            this.panel.btn_close0.addEventListener(egret.TouchEvent.TOUCH_TAP, this.hideChangeMima, this)
            this.panel.szmaBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showSetMima, this)
            this.panel.forgetP.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showChangeMima, this)
            this.panel.cancelM.addEventListener(egret.TouchEvent.TOUCH_TAP, this.cancelbetClick, this)
            this.panel.allBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.allbetClick, this)
            this.panel.submit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.submitMoney, this)
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this)
            this.panel.withdrawal.addEventListener(egret.TouchEvent.TOUCH_TAP, this.withdrawaAndDepositlButtonClick, this)
            this.panel.deposit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.withdrawaAndDepositlButtonClick, this)
            this.panel.outBank_hslider.addEventListener(eui.UIEvent.CHANGE, this.onChangeGold, this);
            this.panel.outBank_hslider.addEventListener(eui.UIEvent.CHANGE_END, this.onChangeGoldEnd, this);
            this.panel.outBank_hslider.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickGoldEnd, this);
            this.panel.eText_Code1.inputType = egret.TextFieldInputType.PASSWORD;
            this.panel.eText_Code1.displayAsPassword = true;
            this.panel.eText_Code.inputType = egret.TextFieldInputType.PASSWORD;
            this.panel.eText_Code.displayAsPassword = true;
            this.panel.cqMoneyM.inputType = egret.TextFieldInputType.PASSWORD;
            this.panel.cqMoneyM.displayAsPassword = true;
            this.panel.eText_Code3.inputType = egret.TextFieldInputType.PASSWORD;
            this.panel.eText_Code3.displayAsPassword = true;


            this.panel.cqMoney.addEventListener(egret.FocusEvent.FOCUS_IN, () => {
                this.panel.cqMoney.text = ""
            }, this)
            this.panel.cqMoney.addEventListener(egret.FocusEvent.FOCUS_OUT, () => {
                if (!RegUtils.isZhengShu(this.panel.cqMoney.text)) {//判断是否正数
                    this.panel.cqMoney.text = "";
                    EffectUtils.textFadeAway("请输入正确的数值！")
                    return;
                }
                //输入的值不得大于可用值
                var maxMoney = null;
                if (this.selectButton == this.panel.withdrawal) {
                    maxMoney = Number(this.panel.bMoney.text)
                }
                if (this.selectButton == this.panel.deposit) {
                    maxMoney = Number(this.panel.xMoney.text)
                }
                if (maxMoney != null) {
                    if (Number(this.panel.cqMoney.text) > maxMoney) {
                        this.panel.cqMoney.text = maxMoney + ""
                    }
                }
                this.panel.cqMoney.text = Global.returnFloat(this.panel.cqMoney.text)//保留两位小数
            }, this)
        }
        public initData(): void {
            this.panel.outBank_hslider.minimum = 0;//定义最小值
            this.panel.outBank_hslider.maximum = 100;//定义最大值
            this.selectButton = this.panel.withdrawal
        }
        /**
         * 初始化面板数据
         */
        /**
 * 初始化面板数据
 */
        //拖动音乐
        private onChangeGold(evt: eui.UIEvent): void {
            this.addGoldMask(evt.target.value);
        }
        //拖动音乐结束
        private onChangeGoldEnd(evt: eui.UIEvent) {
            if (this.selectButton == this.panel.deposit) {
                this.panel.cqMoney.text = ((evt.target.value * Number(this.panel.xMoney.text)) / 100).toFixed(2) + ""
            } else {
                this.panel.cqMoney.text = ((evt.target.value * Number(this.panel.bMoney.text)) / 100).toFixed(2) + ""
            }
        }
        private onEnsureBtnClick(event: egret.TouchEvent): void {
            var _thisObj = this;
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            let pass = this.panel.eText_Code.text;
            let pass2 = this.panel.eText_Code1.text;
            if (pass.length == 0) {
                Global.tipShow("请输入保险箱密码!")
                return;
            }
            if (pass.length < 6) {
                Global.tipShow("密码必须大于6个字符，请重新输入!")
                return;
            }
            if (pass.length > 18) {
                Global.tipShow("密码必须小于18个字符，请重新输入!")
                return;
            }
            if (pass != pass2) {
                Global.tipShow("两次密码输入不一致!")
                return;
            }
            let objc = {
                "dwUserID": GameVO.getVo().dwUserID,
                "szLogonPass": new utils.MD5().hex_md5(GameVO.getVo().szPassword).toUpperCase(),
                "szInsurePass": new utils.MD5().hex_md5(pass).toUpperCase(),
                "szMachineID": ""
            }
            SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HALL, RegDataGame.CMD_GP_UserEnableInsure, objc, true)
        }
        private refresh(date) {
            this.panel.xMoney.text = (date["lUserScore"] / 100).toFixed(2)
            this.panel.bMoney.text = (date["lUserInsure"] / 100).toFixed(2)
        }
        private passSet(date) {
            if (date["cbInsureEnabled"]) {
                GameVO.getVo().cbInsureEnabled = true
                this.panel.szmaP.visible = false
                this.panel.szMima.visible = false
                this.panel.srMima.visible = true
                Global.tipShow(date["szDescribeString"])
            }
            else {
                Global.tipShow(date["szDescribeString"])
            }

        }
        private showChangeMima() {
            this.panel.czmaP.visible = true
        }
        private hideChangeMima() {
            this.panel.czmaP.visible = false
        }
        private showSetMima() {
            this.panel.szmaP.visible = true
        }
        private hideSetMima() {
            this.panel.szmaP.visible = false
        }
        private addGoldMask(value: any) {
            this.panel.bgyyTrack.width = this.TrackWidth * (value / 100)
            this.panel.baifenshu.x = 592 * (value / 100) + 366
            this.panel.baifenshud.text = value + "%"
            if (Number(value) != 0) {
                if (this.selectButton == this.panel.deposit) {
                    this.panel.cqMoney.text = ((Number(value) * Number(this.panel.xMoney.text)) / 100).toFixed(2) + ""
                }
                else {
                    this.panel.cqMoney.text = ((Number(value) * Number(this.panel.bMoney.text)) / 100).toFixed(2) + ""
                }
            } else {
                this.panel.cqMoney.text = ""
            }

        }
        private onClickGoldEnd(evt: egret.TouchEvent) {
            if (evt != null && evt.target instanceof eui.HSlider) {
                if (this.selectButton == this.panel.deposit) {
                    this.panel.cqMoney.text = ((Number(evt.target.value) * Number(this.panel.xMoney.text)) / 100).toFixed(2) + ""
                } else {
                    this.panel.cqMoney.text = ((Number(evt.target.value) * Number(this.panel.bMoney.text)) / 100).toFixed(2) + ""
                }
            }
        }
        private allbetClick(evt: egret.TouchEvent) {
            if (evt != null) {
                this.panel.outBank_hslider.value = 100
                this.addGoldMask(100)
            }
        }
        private cancelbetClick(evt: egret.TouchEvent) {
            if (evt != null) {
                this.panel.outBank_hslider.value = 0
                this.addGoldMask(0)
            }
        }
        private submitMoney(evt: egret.TouchEvent) {
            if (Global.isNumber(this.panel.cqMoney.text)) {
                if (Number(this.panel.cqMoney.text)) {
                    if (evt != null) {
                        if (this.selectButton == this.panel.deposit) {
                            if (Number(this.panel.cqMoney.text) > Number(this.panel.xMoney.text)) {
                                Global.tipShow("您的余额不足!")
                            }
                            else {
                                let objc = {
                                    "dwUserID": GameVO.getVo().dwUserID,
                                    "lSaveScore": Number(this.panel.cqMoney.text) * 100,
                                    "szMachineID": ""
                                }
                                SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HALL, RegDataGame.DBR_GP_UserSaveScore, objc, true);
                                this.panel.cqMoney.text = ""
                            }
                        } else {
                            let pass = this.panel.cqMoneyM.text;
                            if (this.panel.szmaP.visible == true) {
                                if (pass.length == 0) {
                                    Global.tipShow("请输入保险箱密码!")
                                    return;
                                }
                                if (pass.length < 6) {
                                    Global.tipShow("密码必须大于6个字符，请重新输入!")
                                    return;
                                }
                                if (pass.length > 18) {
                                    Global.tipShow("密码必须小于18个字符，请重新输入!")
                                    return;
                                }
                                if (Global.isEmpty(pass)) {
                                    Global.tipShow("请输入密码!")
                                    return;
                                }
                            }

                            if (Number(this.panel.cqMoney.text) > Number(this.panel.bMoney.text)) {
                                Global.tipShow("您的保险箱余额不足!")
                                return;
                            }
                            this.panel.cqMoneyM.text = ""
                            let objc = {
                                "dwUserID": GameVO.getVo().dwUserID,
                                "lTakeScore": Number(this.panel.cqMoney.text) * 100,
                                "szPassword": this.panel.srMima.visible == true ? new utils.MD5().hex_md5(pass).toUpperCase() : "",
                                "szMachineID": ""
                            }
                            SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.HALL, RegDataGame.DBR_GP_UserTakeScore, objc, true);
                            this.panel.cqMoney.text = ""
                        }
                    }
                } else {
                    Global.tipShow("请输入正确金额")
                }
            } else {
                Global.tipShow("请输入正确金额")
            }

        }
        //修改密码点击事件
        private onEnsureBtnClickl(event: egret.TouchEvent): void {
            var _thisObj = this;
            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            let Identifycode = this.panel.eText_Identifycode.text
            let pass = this.panel.eText_Code3.text;

            if (Global.isEmpty(Identifycode)) {
                Global.tipShow("请输入验证码!")
                return;
            }
            if (pass.length == 0) {
                Global.tipShow("请输入保险箱密码!")
                return;
            }
            if (pass.length < 6) {
                Global.tipShow("密码必须大于6个字符，请重新输入!")
                return;
            }
            if (pass.length > 18) {
                Global.tipShow("密码必须小于18个字符，请重新输入!")
                return;
            }

            EffectUtils.showWaritPanel();
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/H5Interface/ModifyUserInsurePass?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time + "&PhoneNum=" + GameVO.getVo().szMobilePhone + "&NewPassword=" + new utils.MD5().hex_md5(pass).toUpperCase()
                + "&PhoneCode=" + Identifycode + "&AuthToken=" + new utils.MD5().hex_md5(GameVO.getVo().szMobilePhone + "NH11MqbKJ5hVr9do").toUpperCase() + "&PlatFormName=yuezhong", egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    EffectUtils.hideWaritPanel();
                    if (obj.code == 0) {
                        _thisObj.closePanel(1);
                        Global.tipShow("密码修改成功！")
                    }
                    else {
                        Global.tipShow(obj.mess)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            // SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_HALL, RegDataGame.CMD_MB_RegAccountsSpreader, obj, true)
            // GameVO.getVo().registeAcount = true;
        }
        //获取短信验证码点击事件
        private onGetIdentufyCodeBtnClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            EffectUtils.playDim(event.currentTarget);
            this.panel.btn_getIdentifycode.enabled = false;
            let time = 120;
            this.panel.identify_rem.text = "重新获取" + time
            this.panel.identify_rem.visible = true;
            this.panel.identify_rem;
            let t = egret.setInterval(() => {
                time--
                this.panel.identify_rem.text = "重新获取" + time
                if (time == 0) {
                    this.panel.btn_getIdentifycode.enabled = true;
                    this.panel.identify_rem.visible = false;
                    egret.clearInterval(t)
                }

            }, this, 1000)
            InstructionsEnum.GAME_INTERVAL_LIST.push(t);
            this.panel.identify_rem
            var request = new egret.HttpRequest();
            var paramas = {
                "OptionType": 0,
                "CountryCode": "86",
                "PhoneNum": GameVO.getVo().szMobilePhone,
                "Mac": "221bd4b23889e4701f7db1f4a86dccb"
            };
            request.responseType = egret.HttpResponseType.TEXT;
            request.open("http://59.188.233.197:1000/GameWebApplication/GetPhoneCode.ashx", egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", 'text/plain;charset=UTF-8');
            request.send(JSON.stringify(paramas));
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);

        }
        //存取款切换
        private selectButton: eui.Button = null;
        private withdrawaAndDepositlButtonClick(event: egret.TouchEvent): void {
            this.selectButton = event.currentTarget;
            this.panel.outBank_hslider.value = 0
            this.addGoldMask(0)
            if (this.selectButton == this.panel.deposit) {
                this.panel.srMima.visible = false;
                this.panel.szMima.visible = false;
                this.panel.deposit.enabled = false;
                this.panel.deposit.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.withdrawaAndDepositlButtonClick, this)
                this.panel.cqkImg.source = "bxx_qk_t"
                this.panel.withdrawal.enabled = true;
                this.panel.withdrawal.addEventListener(egret.TouchEvent.TOUCH_TAP, this.withdrawaAndDepositlButtonClick, this)
            }
            if (this.selectButton == this.panel.withdrawal) {
                if (GameVO.getVo().cbInsureEnabled) {
                    this.panel.srMima.visible = true;
                }
                else {
                    this.panel.szMima.visible = true;
                }
                this.panel.withdrawal.enabled = false;
                this.panel.withdrawal.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.withdrawaAndDepositlButtonClick, this)
                this.panel.cqkImg.source = "bxx_ck_t"
                this.panel.deposit.enabled = true;
                this.panel.deposit.addEventListener(egret.TouchEvent.TOUCH_TAP, this.withdrawaAndDepositlButtonClick, this)
            }
        }


        private initBankInfo(date): void {
            this.panel.xMoney.text = (date["lUserScore"] / 100).toFixed(2)
            this.panel.bMoney.text = (date["lUserInsure"] / 100).toFixed(2)
        }
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }

    }
}
